package com.onesports.intelligent.k12.polarlight.service.evaluation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.domain.dto.market.EducationEvaluationRecordAddDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.market.EducationEvaluationRecordPageDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.ClassInfo;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.EducationEvaluationRecord;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.EducationEvaluationRecordDetailVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.EducationEvaluationRecordPageVO;
import com.onesports.intelligent.k12.polarlight.mapper.course.ClassInfoMapper;
import com.onesports.intelligent.k12.polarlight.mapper.market.EducationEvaluationRecordMapper;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author FPH
 * @since 2022年7月18日15:35:45
 */
@Service
@RequiredArgsConstructor
public class EducationEvaluationRecordService extends ServiceImpl<EducationEvaluationRecordMapper, EducationEvaluationRecord> {
    private final EducationEvaluationRecordMapper educationEvaluationRecordMapper;

    private final ClassInfoMapper classInfoMapper;

    /**
     * 机构端-教学管理-教学评价 分页
     * @param dto
     * @return
     */
    public IPage<EducationEvaluationRecordPageVO> getPage( EducationEvaluationRecordPageDTO dto){
        Page<EducationEvaluationRecordPageVO> page = new Page<>(dto.getPage(), dto.getRow());
        return educationEvaluationRecordMapper.getPage(page,dto, SecurityUtils.getOrganizationId());
    }

    /**
     * 机构端-教学管理-教学评价 详情
     * @param id
     * @return
     */
    public EducationEvaluationRecordDetailVO getDetail(String id){
        return educationEvaluationRecordMapper.getDetail(id,SecurityUtils.getOrganizationId());
    }

    /**
     * 添加教学评价
     * @param dto
     * @return
     */
    public Boolean addEvaluation( EducationEvaluationRecordAddDTO dto){
        List<EducationEvaluationRecord> list = list(new LambdaQueryWrapper<EducationEvaluationRecord>()
                .eq(EducationEvaluationRecord::getStudentId, dto.getStudentId())
                .eq(EducationEvaluationRecord::getClassId, dto.getClassId())
                .eq(EducationEvaluationRecord::getCoachId,dto.getCoachId()));
        if(CollectionUtils.isNotEmpty(list)){
            throw new BusinessException("当前学员已评价");
        }
        EducationEvaluationRecord educationEvaluationRecord = new EducationEvaluationRecord();
        BeanUtils.copyProperties(dto,educationEvaluationRecord);
        educationEvaluationRecord.setEvaluationTime(new Date());
        //通过班级ID找到机构ID
        ClassInfo classInfo = classInfoMapper.selectById(dto.getClassId());
        if(classInfo==null){
            throw new BusinessException("班级不存在");
        }
        educationEvaluationRecord.setOrganizationId(classInfo.getOrganizationId());
        return save(educationEvaluationRecord);
    }
}
